package com.li.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.SweepGradient;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.ndboo.reslibrary.R;

/**
 * Author: Li
 * Date: 2017/5/3
 * Description: 加载效果
 */

public class LoadingView extends View {

    private static final float DEFAULT_TEXT_SIZE = 12f;
    private static final int DEFAULT_TEXT_COLOR = Color.WHITE;
    private static final float DEFAULT_INNER_PADDING = 5f;
    private static final float DEFAULT_OUTER_PADDING = 10f;
    private static final float DEFAULT_STROKE_WIDTH = 3f;
    private static final int DEFAULT_START_COLOR = Color.RED;
    private static final int DEFAULT_CENTER_COLOR = Color.WHITE;
    private static final int DEFAULT_END_COLOR = Color.RED;
    private static final float DEFAULT_START_POS = 0f;
    private static final float DEFAULT_CENTER_POS = 0.5f;
    private static final float DEFAULT_END_POS = 1f;
    private static final int DEFAULT_OUTER_BACKGROUND = Color.parseColor("#88000000");
    private static final float DEFAULT_X_CORNER = 5f;
    private static final float DEFAULT_Y_CORNER = 5f;
    private static final float DEFAULT_DEGREES_STEP = 3f;
    private static final int DEFAULT_DEGREES_DURATION = 3;
    private Paint mTextPaint;
    private Paint mCirclePaint;
    private Paint mRectPaint;
    private float mTextWidth;
    private int mRotate = 0;
    private RectF mRectF;
    private String mText;
    private float mTextSize;
    private int mTextColor;
    private float mInnerPadding;
    private float mOuterPadding;
    private float mStrokeWidth;
    private int mStartColor;
    private int mCenterColor;
    private int mEndColor;
    private float mStartPos;
    private float mCenterPos;
    private float mEndPos;
    private int mOuterBackground;
    private float mXCorner;
    private float mYCorner;
    private float mDegreesStep;
    private float mDegreesDuration;
    private int mViewWidth;
    private int mViewHeight;

    public LoadingView(Context context) {
        this(context, null);
    }

    public LoadingView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.LoadingView);

        mText = array.getString(R.styleable.LoadingView_text);
        mTextSize = array.getDimension(R.styleable.LoadingView_text_size, sp2px(DEFAULT_TEXT_SIZE));
        mTextColor = array.getColor(R.styleable.LoadingView_text_color, DEFAULT_TEXT_COLOR);
        mInnerPadding = array.getDimension(R.styleable.LoadingView_inner_padding, dp2px(DEFAULT_INNER_PADDING));
        mOuterPadding = array.getDimension(R.styleable.LoadingView_outer_padding, dp2px(DEFAULT_OUTER_PADDING));
        mStrokeWidth = array.getDimension(R.styleable.LoadingView_stroke_width, dp2px(DEFAULT_STROKE_WIDTH));

        mStartColor = array.getColor(R.styleable.LoadingView_start_color, DEFAULT_START_COLOR);
        mCenterColor = array.getColor(R.styleable.LoadingView_center_color, DEFAULT_CENTER_COLOR);
        mEndColor = array.getColor(R.styleable.LoadingView_end_color, DEFAULT_END_COLOR);
        mStartPos = array.getFloat(R.styleable.LoadingView_start_position, DEFAULT_START_POS);
        mCenterPos = array.getFloat(R.styleable.LoadingView_center_position, DEFAULT_CENTER_POS);
        mEndPos = array.getFloat(R.styleable.LoadingView_end_position, DEFAULT_END_POS);

        mOuterBackground = array.getColor(R.styleable.LoadingView_outer_background, DEFAULT_OUTER_BACKGROUND);
        mXCorner = array.getDimension(R.styleable.LoadingView_x_corner, dp2px(DEFAULT_X_CORNER));
        mYCorner = array.getDimension(R.styleable.LoadingView_y_corner, dp2px(DEFAULT_Y_CORNER));

        mDegreesStep = array.getFloat(R.styleable.LoadingView_degrees_step, DEFAULT_DEGREES_STEP);
        mDegreesDuration = array.getInt(R.styleable.LoadingView_degrees_duration, DEFAULT_DEGREES_DURATION);
        array.recycle();
        init();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        if (widthMode == MeasureSpec.AT_MOST || heightMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension((int) (mRectF.width() + 1f), (int) (mRectF.height() + 1f));
        } else {
            setMeasuredDimension(Math.max((int) (mRectF.width() + 1f), MeasureSpec.getSize(widthMeasureSpec)),
                    Math.max((int) (mRectF.height() + 1f), MeasureSpec.getSize(heightMeasureSpec)));
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mViewWidth = getWidth();
        mViewHeight = getHeight();
    }

    private void init() {
        mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mTextPaint.setColor(mTextColor);
        mTextPaint.setTextSize(mTextSize);

        mText=TextUtils.isEmpty(mText)?"Loading":mText;
        mTextWidth = mTextPaint.measureText(mText);
        mCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mCirclePaint.setStrokeWidth(mStrokeWidth);
        mCirclePaint.setStyle(Paint.Style.STROKE);
        SweepGradient sweepGradient = new SweepGradient(0, 0,
                new int[]{mStartColor, mCenterColor, mEndColor},
                new float[]{mStartPos, mCenterPos, mEndPos});
        mCirclePaint.setShader(sweepGradient);


        mRectPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mRectPaint.setColor(mOuterBackground);

        float mRectFRadius = mTextWidth / 2 + mInnerPadding + mStrokeWidth + mOuterPadding;
        mRectF = new RectF(-mRectFRadius, -mRectFRadius, mRectFRadius, mRectFRadius);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(mViewWidth / 2, mViewHeight / 2);


        canvas.drawRoundRect(mRectF, mXCorner, mYCorner, mRectPaint);
        canvas.drawText(mText, -mTextWidth / 2, Math.abs(mTextPaint.getFontMetrics().ascent +
                mTextPaint.getFontMetrics().descent) / 2, mTextPaint);

        mRotate += mDegreesStep;
        mRotate = mRotate > 360 ? 0 : mRotate;
        canvas.rotate(mRotate);

        canvas.drawCircle(0, 0, mTextWidth / 2 + mInnerPadding, mCirclePaint);
        postInvalidateDelayed((long) mDegreesDuration);
    }

    private float sp2px(float sp) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, getResources().getDisplayMetrics());
    }

    private float dp2px(float dp) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
    }

    public void setText(String text){
        mText=text;
        init();
    }
}
